﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DotNet.Model;
using DotNet.BaseManager;
using KingRoad.EPM.Web.Util;
//using KingRoad.EPM.InvestmeEntity;

using DotNet.Service;
using DotNet.Model.MyEntity;
using MySoft.Data;
using KingRoad.EPM.Web.ViewModels;

namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    public class LogController : Controller
    {
        //
        // GET: /Permission/Log/

        public ActionResult Index()
        {
            return View();
        }
        //获得用户访问情况
        public ActionResult GetLogList(DotNet.Model.MyEntity.QueryBase query, string check, string txtSearch)
        {
            IDataPage<DataTable> data = ServiceManager.Instance.LogService.GetLog(query, check, txtSearch);
            var jqGridData = WebUtility.ToJqGridData<DotNet.Model.MyEntity.LogUserEntityExtension>(data);
            return Json(jqGridData);
        }
        //删除操作
        public ActionResult Delete(string ids, string type)
        {
            int state = 1;
            if (!string.IsNullOrEmpty(ids))
            {
                if (ids == "0")
                {
                    if (type == "Exception")
                        ServiceManager.Instance.LogService.Toruncat();
                    else
                        ServiceManager.Instance.LogService.Truncate();
                    state = 0;
                }
                else
                {
                    if (ids.EndsWith(","))
                        ids = ids.Substring(0, ids.Length - 1);
                    if (type == "Exception")
                    {
                        if (ServiceManager.Instance.LogService.BatchDelete(ids) > 0)
                            state = 0;
                    }
                    else
                    {
                        if (ServiceManager.Instance.LogService.DeleteLog(ids) > 0)
                            state = 0;
                    }
                }
            }
            return Json(new CallbackStatus
            {
                message = state == 0 ? "操作成功！" : "操作失败",
                statusCode = state == 0 ? "200" : "300",
                callbackType = "closeCurrent"
            });
        }
        #region 按用户查询
        //按用户查询
        public ActionResult QueryUser(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                ViewBag.Id = UserService.CurrUserInfo.Id;
                ViewBag.UserName = UserService.CurrUserInfo.UserName;
            }
            else
            {
                Base_User user = ServiceManager.Instance.UserService.GetUserEntityByID(int.Parse(id));
                ViewBag.Id = user.Id;
                ViewBag.UserName = user.UserName;
            }
            ViewBag.startDate = DateTime.Now.ToString("yyyy-MM-dd");
            ViewBag.endDate = DateTime.Now.ToString("yyyy-MM-dd");
            return View();
        }
        public ActionResult QueryByUser(QueryBase query, string userid, string startDate, string endDate)
        {
            List<Base_Log> logs = ServiceManager.Instance.LogService.GetLogByUserID(userid, startDate, endDate) as List<Base_Log>;
            var jqGridData = WebUtility.TojqGridData<Base_Log>(logs, query);
            return Json(jqGridData);
        }
        public ActionResult SelectUser(string id)
        {
            ViewBag.Flag = id;
            return View();
        }
        public ActionResult SelectSingleUser(QueryBase query)
        {
            DotNet.Model.MyEntity.QueryBase q = new DotNet.Model.MyEntity.QueryBase
            {
                _search = query._search,
                page = query.page,
                records = query.records,
                rows = query.rows,
                sidx = query.sidx,
                sord = query.sord,
                total = query.total
            };
            var users = ServiceManager.Instance.UserService.GetDT(q);
            var jqGridData = WebUtility.TojqGridData<Base_User>(users, query);
            return Json(jqGridData);
        }
        #endregion
        #region 按模块查询
        //按模块查询
        public ActionResult QueryModule()
        {
            ViewBag.startDate = DateTime.Now.ToString("yyyy-MM-dd");
            ViewBag.endDate = DateTime.Now.ToString("yyyy-MM-dd");
            return View();
        }
        public ActionResult QueryByModule(DotNet.Model.MyEntity.QueryBase query, string moduleId, string startDate, string endDate)
        {
            IDataPage<IList<Base_Log>> logs = ServiceManager.Instance.LogService.GetLogByModuleId(moduleId, startDate, endDate, query);
            var jqGridData = WebUtility.TojqGridData(logs);
            return Json(jqGridData);
        }
        public ActionResult SelectModule(string id)
        {
            ViewBag.Flag = id;
            return View();
        }
        public ActionResult SelectSingleModule(QueryBase query)
        {
            List<BaseModuleEntity> list = ServiceManager.Instance.ModuleService.GetPjtModules() as List<BaseModuleEntity>;
            return Json(list);
        }
        #endregion
        #region 按日期查询
        //按日期查询
        public ActionResult QueryDate()
        {
            ViewBag.startDate = DateTime.Now.ToString("yyyy-MM-dd");
            ViewBag.endDate = DateTime.Now.ToString("yyyy-MM-dd");
            return View();
        }
        public ActionResult QueryByDate(DotNet.Model.MyEntity.QueryBase query, string userId, string moduleId, string startDate, string endDate)
        {
            IDataPage<IList<Base_Log>> logs = ServiceManager.Instance.LogService.GetDTByDate(UserService.CurrUserInfo, startDate, endDate, userId, moduleId, query);
            var jqGridData = WebUtility.TojqGridData(logs);
            return Json(jqGridData);
        }
        #endregion
        #region 系统异常情况查询
        public ActionResult QueryException()
        {
            return View();
        }
        public ActionResult QueryByException(DotNet.Model.MyEntity.QueryBase query)
        {
            IDataPage<IList<Base_Exception>> exception = ServiceManager.Instance.ExceptionService.GetDT(UserService.CurrUserInfo, query);
            var jqGridData = WebUtility.TojqGridData(exception);
            return Json(jqGridData);
        }
        public ActionResult ExceptionDetails(string id)
        {
            return View();
        }
        #endregion
    }
}
